﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Afall, $ionicSlideBoxDelegate, GoPage, GoDialog, Auth, $window, $timeout, $stateParams, $ionicScrollDelegate, FileSystem, $ionicModal, $ionicPopup) {
		$scope.input = new Object();
		$scope.input.order_id = '';
		$scope.input.goods_name = '';
		$scope.input.time = '';
		$scope.time = '';
		$scope.type = '';
		var pay_name = new Array(6);
		//滑动切换
		$scope.nPage = 0;
		$scope.input.checked = false;
		$scope.isLoad = false;
		$scope.count_f = null;
		$scope.count_s = null;
		$scope.count_t = null;
		$scope.count_fo = null;
		$scope.count_fi = null;
		$scope.all = new Array();
		$scope.comment = new Array();
		$scope.shiped = new Array();
		$scope.ship = new Array();
		$scope.nopayed = new Array();
		$scope.order_id = '';
		$scope.act_type = '';
		$scope.is_app = false;
		
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins) {
				$scope.is_app = true;
				if(ionic.Platform.isIOS()) {
					lan = 20;
				}
			}
			var all = $window.innerHeight;
			$scope.afall_myObj1 = {
				"height": all - 44*2 - lan + "px"
			};
			$scope.afall_myObjAll = {
				"height": all - 44*2 - 55 - lan + "px"
			};
			$scope.selectTab = $stateParams.type;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectTab);
			$scope.init_data();
		}, false);
		
		$scope.activeSlide = function(index) {
			$scope.showtop999 = "hide";
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		
		//滑动切换
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
			$scope.showtop999 = "hide";
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//多个事件
		$scope.onshow_waitPay = function(index, data, errorMsg, confirmMsg) {
			$scope.isLoad = true;
			if(errorMsg){//有错误提示，就不触发以下事件
				GoDialog.alert("操作失败",errorMsg);
				return;
			}
			switch(index) {
				case 0://订单详情
					GoPage.go_states("buyerpayment", {
						order_id: data.order_id,
						tab: $scope.selectTab
					});
					break;
				case 1://取消订单
					GoDialog.confirm("取消订单",confirmMsg,function(){
						$scope.cancel_order(data.order_id);
					})
					break;
				case 2://确认收货
					GoDialog.confirm("确认收货",confirmMsg,function(){
						$scope.go_finish(data.order_id);
					})
					break;
				case 3:
					$scope.delay_goods(data.order_id);
					break;
				case 4:
					$scope.go_afback(data.order_id);
					break;
				case 5:
					$scope.notice_seller(data.order_id);
					break;
				case 6:
					$scope.go_afback(data.order_id);
					break;
				case 7:
					$scope.go_afback(data.order_id);
					break;
				case 8:
					$scope.go_afback(data.order_id);
					break;
				case 9:
					$scope.go_afback(data.order_id);
					break;
				case 10:
					$scope.go_afback(data.order_id);
					break;
				case 11:
					$scope.go_aftalk(data.order_id, false, data.order_type_id, data.act_id);
					break;
				case 12:
					$scope.go_aftalkagain(data.order_id);
					break;
				case 13:
					$scope.go_pay(data.order_id);
					break;
				case 14:
					$scope.doShowFile(data.order_id, data.act_file_id, data.order_type_id);
					break;
				case 15:
					$scope.file_download(data.act_file_id);
					break;
				case 16://删除订单
					GoDialog.confirm("删除订单",confirmMsg,function(){
						$scope.go_delete(data.order_id);
					})
					break;
				case 17://查看物流
					GoPage.go_states("orderlogistics",{'order_id':data.order_id});
					break;	
			}
			$timeout(function() {
				$scope.isLoad = false;
			}, 1000);
		}
		
		//退款退货
		$scope.onshow_refund = function() {
			GoPage.go_states("refund");
		};
		
		$scope.onshow_first = function(store_id,order_type_id) {
			if(order_type_id=="gift"){
				return;
			}	
			GoPage.go_states("first", {
				"store_id": store_id
			});
		};
		
		//取消订单
		$scope.cancel_order = function(order_id) {
			Afall.cancel_order(order_id, function(data) {
				if(data.success == true) {
					$scope.get_list('all');
					$scope.get_list('nopayed');
					//					$scope.init_data();
					GoDialog.toast("取消订单成功");
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		};
		//确认收货
		$scope.go_finish = function(order_id) {
			Afall.go_finish(order_id, function(data) {
				if(data.success == true) {
					$scope.get_list('all');
					$scope.get_list('ship');
					$scope.get_list('shiped');
					$scope.get_list('comment');
					GoDialog.toast("确认收货成功");
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		};
		//删除订单
		$scope.go_delete = function(order_id){
			Afall.go_delete(order_id, function(data) {
				if(data.success == true) {
					$scope.get_list('all');
					$scope.get_list('ship');
					$scope.get_list('shiped');
					$scope.get_list('comment');
					GoDialog.toast(data.msg);
				} else{
					GoDialog.alert("提示",data.msg);
				}
			})
		}
		
		$scope.delay_goods = function(order_id) {
			Afall.go_extend(order_id, function(data) {
				if(data.success == true) {
					$scope.get_list('all');
					$scope.get_list('shiped');
					GoDialog.toast("延长收货成功");
				} else {
					GoDialog.toast(data.msg);
				}
			})
		};
		$scope.notice_seller = function(order_id, store_id) {
			Afall.go_remind(order_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("提醒卖家成功");
				} else {
					GoDialog.toast(data.msg);
				}
			})
		};
		$scope.go_afback = function(order_id) {
			GoPage.go_states("afback", {
				"order_id": order_id
			});
		};
		$scope.go_aftalk = function(order_id, is_new, act_type, act_id) {
			GoPage.go_states("aftalk", {
				"order_id": order_id,
				"is_new": is_new,
				"act_type": act_type,
				"act_id": act_id
			});
		};
		$scope.go_aftalkagain = function(order_id) {
			GoPage.go_states("aftalkagain", {
				"id": order_id			
			});
		};
		
		$scope.go_pay = function(order_id) {
			var order = new Array();
			order[0] = order_id;
			order = JSON.stringify(order);
			GoPage.go_states("afpay", {
				"order_id": order
			});
		};
		$scope.send_file = function(order_id, act_type, file, filename) {
			Afall.upload_file(order_id, act_type, file, filename, function(data) {
				GoDialog.toast(data.msg)
			})
		}
		
		$scope.get_list = function(type) {
			Afall.get_list('', '', '', '', type, 1, function(data) {
//				console.log(type)
//				console.log(data)
				if(data != null) {
					if(data.data != undefined) {
						if(type == 'all') {
							$scope.all = data;
							$scope.input.all_nPage = 1;
							$scope.firstHide[0] = true;//有数据的时候，必须放在这里，否则没效果，因为赋值比较耗时
						} else if(type == 'nopayed') {
							$scope.nopayed = data;
							$scope.input.nopayed_nPage = 1;
							$scope.firstHide[1] = true;
						} else if(type == 'ship') {
							$scope.ship = data;
							$scope.input.ship_nPage = 1;
							$scope.firstHide[2] = true;
						} else if(type == 'shiped') {
							$scope.shiped = data;
							$scope.input.shiped_nPage = 1;
							$scope.firstHide[3] = true;
						} else if(type == 'comment') {
							$scope.comment = data;
							$scope.input.comment_nPage = 1;
							$scope.firstHide[4] = true;
						}
					} else {
						if(type == 'all') {
							$scope.all = new Object();
							$scope.input.all_nPage = 1;
							$scope.firstHide[0] = true;
						} else if(type == 'nopayed') {
							$scope.nopayed = new Object();
							$scope.input.nopayed_nPage = 1;
							$scope.firstHide[1] = true;
						} else if(type == 'ship') {
							$scope.ship = new Object();
							$scope.input.ship_nPage = 1;
							$scope.firstHide[2] = true;
						} else if(type == 'shiped') {
							$scope.shiped = new Object();
							$scope.input.shiped_nPage = 1;
							$scope.firstHide[3] = true;
						} else if(type == 'comment') {
							$scope.comment = new Object();
							$scope.input.comment_nPage = 1;
							$scope.firstHide[4] = true;
						}
//						$scope.firstHide = [true,true,true,true,true];//不能这样
					}
				} else {
					if(type == 'all') {
						$scope.all = new Object();
						$scope.input.all_nPage = 1;
						$scope.firstHide[0] = true;
					} else if(type == 'nopayed') {
						$scope.nopayed = new Object();
						$scope.input.nopayed_nPage = 1;
						$scope.firstHide[1] = true;
					} else if(type == 'ship') {
						$scope.ship = new Object();
						$scope.input.ship_nPage = 1;
						$scope.firstHide[2] = true;
					} else if(type == 'shiped') {
						$scope.shiped = new Object();
						$scope.input.shiped_nPage = 1;
						$scope.firstHide[3] = true;
					} else if(type == 'comment') {
						$scope.comment = new Object();
						$scope.input.comment_nPage = 1;
						$scope.firstHide[4] = true;
					}
//					$scope.firstHide = [true,true,true,true,true];
				}
			});
		};
		$scope.init_data = function() {
			$scope.firstHide = [false,false,false,false,false];//防止一开始就显示“找不到订单”
			//			$scope.init_pay_name();
			$scope.get_list('all');
			//			$scope.get_list('finish');
			$scope.get_list('nopayed');
			$scope.get_list('ship');
			$scope.get_list('shiped');
			$scope.get_list('comment');
			//			$scope.get_list('dead');
		};
		
		//搜索
		$scope.doSearch = function() {
			var type = "all";
			Afall.get_list('', $scope.input.order_id, $scope.input.goods_name, $scope.input.time, type, 1, function(data) {
					if(data != null) {
						$scope.all = data;
					} else
						$scope.all = new Object();
					$scope.input.all_nPage = 1;
				$timeout(function() {//重置显示高度
					document.getElementById(type).style['height'] = $scope.afall_myObjAll.height;
				}, 300);
				$scope.doScrollToTop('all-handle');
			})
		};
		
		
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			var type = 'all';
			$scope.isLoad = true;
			if($scope.selectTab == 1) {
				type = 'nopayed';
			} else if($scope.selectTab == 2) {
				type = 'ship';
			} else if($scope.selectTab == 3) {
				type = 'shiped';
			} else if($scope.selectTab == 4) {
				type = 'comment';
			}

			Afall.get_list('', $scope.input.order_id, $scope.input.goods_name, $scope.time, type, 1, function(data) {
				if(type == 'all') {
					if(data != null) {
						$scope.all = data;
					} else
						$scope.all = new Object();
					$scope.input.all_nPage = 1;
				} else if(type == 'nopayed') {
					$scope.input.checked = false;
					if(data != null) {
						$scope.nopayed = data;
					} else
						$scope.nopayed = new Object();
					$scope.input.nopayed_nPage = 1;
				} else if(type == 'ship') {
					if(data != null) {
						$scope.ship = data;
					} else
						$scope.ship = new Object();
					$scope.input.ship_nPage = 1;
				} else if(type == 'shiped') {
					if(data != null) {
						$scope.shiped = data;
					} else
						$scope.shiped = new Object();
					$scope.input.shiped_nPage = 1;
				} else {
					if(data != null) {
						$scope.comment = data;
					} else
						$scope.comment = new Object();
					$scope.input.comment_nPage = 1;
				}

			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
		};
		$scope.is_contains = function(str, cp_str) {
			if(cp_str.is_contains(str)) {
				return true;
			}
			return false;
		};
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			var type = 'all';
			if($scope.selectTab == 0) {
				type = 'all'
				if($scope.all.data == undefined)
					$scope.nPage = 1;
				else {
					if($scope.all.page.count <= $scope.all.data.length) {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						if($scope.all.page.count < $scope.input.all_nPage * 10) {
							$scope.input.all_nPage -= 1;
							$scope.count_f = $scope.input.all_nPage * 10;
						} else {
							$scope.count_f = null;
						}
					}
					$scope.nPage = $scope.input.all_nPage + 1;
				}

			} else if($scope.selectTab == 1) {
				type = 'nopayed';
				if($scope.nopayed.data == undefined)
					$scope.nPage = 1;
				else {
					if($scope.nopayed.page.count <= $scope.nopayed.data.length) {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						if($scope.nopayed.page.count < $scope.input.nopayed_nPage * 10) {
							$scope.input.nopayed_nPage -= 1;
							$scope.count_s = $scope.input.nopayed_nPage * 10;
						} else {
							$scope.count_s = null;
						}
					}
					$scope.nPage = $scope.input.nopayed_nPage + 1;
				}
			} else if($scope.selectTab == 2) {
				type = 'ship';
				if($scope.ship.data == undefined)
					$scope.nPage = 1;
				else {
					if($scope.ship.page.count <= $scope.ship.data.length) {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						if($scope.ship.page.count < $scope.input.ship_nPage * 10) {
							$scope.input.ship_nPage -= 1;
							$scope.count_t = $scope.input.ship_nPage * 10;
						} else {
							$scope.count_t = null;
						}
					}
					$scope.nPage = $scope.input.ship_nPage + 1;
				}
			} else if($scope.selectTab == 3) {
				type = 'shiped';
				if($scope.shiped.data == undefined)
					$scope.nPage = 1;
				else {
					if($scope.shiped.page.count <= $scope.shiped.data.length) {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						if($scope.shiped.page.count < $scope.input.shiped_nPage * 10) {
							$scope.input.shiped_nPage -= 1;
							$scope.count_fo = $scope.input.shiped_nPage * 10;
						} else {
							$scope.count_fo = null;
						}
					}
					$scope.nPage = $scope.input.shiped_nPage + 1;
				}
			} else if($scope.selectTab == 4) {
				type = 'comment';
				if($scope.comment.data == undefined)
					$scope.nPage = 1;
				else {
					if($scope.comment.page.count <= $scope.comment.data.length) {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						if($scope.comment.page.count < $scope.input.comment_nPage * 10) {
							$scope.input.comment_nPage -= 1;
							$scope.count_ff = $scope.input.comment_nPage * 10;
						} else {
							$scope.count_ff = null;
						}
					}
					$scope.nPage = $scope.input.comment_nPage + 1;
				}
			}
			Afall.get_list('', $scope.input.order_id, $scope.input.goods_name, $scope.time, type, $scope.nPage, function(data) {
				if(type == 'all') {
					if(data != null) {
						if($scope.all.data != undefined) {
							if($scope.count_f != null)
								$scope.all.data.splice($scope.count_f, $scope.all.page.count - $scope.count_f);
							$scope.all.data = $scope.all.data.concat(data.data);
							$scope.input.all_nPage = $scope.nPage;
						} else {
							$scope.all = data;
							$scope.input.all_nPage = 1;
						}
					}
				} else if(type == 'nopayed') {
					$scope.input.checked = false;
					if(data != null) {
						if($scope.nopayed.data != undefined) {
							if($scope.count_s != null)
								$scope.nopayed.data.splice($scope.count_s, $scope.nopayed.page.count - $scope.count_s);
							$scope.nopayed.data = $scope.nopayed.data.concat(data.data);
							$scope.input.nopayed_nPage = $scope.nPage;
						} else {
							$scope.nopayed = data;
							$scope.input.nopayed_nPage = 1;
						}
					}
				} else if(type == 'ship') {
					if(data != null) {
						if($scope.ship.data != undefined) {
							if($scope.count_t != null)
								$scope.ship.data.splice($scope.count_t, $scope.ship.page.count - $scope.count_t);
							$scope.ship.data = $scope.ship.data.concat(data.data);
							$scope.input.ship_nPage = $scope.nPage;
						} else {
							$scope.ship = data;
							$scope.input.ship_nPage = 1;
						}
					}
				} else if(type == 'shiped') {
					if(data != null) {
						if($scope.shiped.data != undefined) {
							if($scope.count_fo != null)
								$scope.shiped.data.splice($scope.count_fo, $scope.shiped.page.count - $scope.count_fo);
							$scope.shiped.data = $scope.shiped.data.concat(data.data);
							$scope.input.shiped_nPage = $scope.nPage;
						} else {
							$scope.shiped = data;
							$scope.input.shiped_nPage = 1;
						}
					}
				} else {
					if(data != null) {
						if($scope.comment.data != undefined) {
							if($scope.count_ff != null)
								$scope.comment.data.splice($scope.count_ff, $scope.comment.page.count - $scope.count_ff);
							$scope.comment.data = $scope.comment.data.concat(data.data);
							$scope.input.comment_nPage = $scope.nPage;
						} else {
							$scope.comment = data;
							$scope.input.comment_nPage = 1;
						}
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
		};
		
		//全选/反选
		$scope.check_all = function() {
			if(!$scope.nopayed.data){
				GoDialog.alert("提示","请先去下单吧！")
				return;
			}
			if($scope.input.checked) {
				for(var i = 0; i < $scope.nopayed.data.length; i++) {
					$scope.nopayed.data[i].check = false;
				}
				$scope.input.checked = false;
			} else {
				$scope.input.checked = true;
				for(var i = 0; i < $scope.nopayed.data.length; i++) {
					$scope.nopayed.data[i].check = true;
				}
			}
		};
		//单选
		$scope.is_check = function(index) {
			if($scope.nopayed.data[index].check) {
				$scope.nopayed.data[index].check = false;
			} else {
				$scope.nopayed.data[index].check = true;
			}
			$scope.checkIsAll();
		};
		//检查是否已经全选，控制样式
		$scope.checkIsAll = function(){
			$scope.input.checked = true;
			for(var i = 0; i < $scope.nopayed.data.length; i++) {
				if(!$scope.nopayed.data[i].check){
					$scope.input.checked = false;
					break;
				}
			}
		}
		
		$scope.more_pay = function() {
			if(!$scope.nopayed.data){
				GoDialog.alert("提示","请先去下单吧！")
				return;
			}
			var order_id = new Array();
			for(var i = 0; i < $scope.nopayed.data.length; i++) {
				if($scope.nopayed.data[i].check == true) {
					order_id[order_id.length] = $scope.nopayed.data[i].order_id;
				}
			}
			if(order_id.length == 0) {
				GoDialog.toast("请选择订单再合并支付！");
				return;
			} else {
				order_id = JSON.stringify(order_id);
				GoPage.go_states("afpay", {
					"order_id": order_id
				});
			}
		};
		//滚动侦听
		$scope.doShowTop = function(name, index) {
			var delegate = $ionicScrollDelegate.$getByHandle(name);
			if(delegate.getScrollPosition() == undefined) return;
			var scrollTop = delegate.getScrollPosition().top;
			//分类
			$scope.$apply(function() {
				if($window.innerHeight <= delegate.getScrollPosition().top) {
					$scope.showtop999 = "show";
				} else {
					$scope.showtop999 = "hide";
				}
			});
		};
		
		//回到顶部
		$scope.doScrollToTop = function(name, index) {
			$ionicScrollDelegate.$getByHandle(name).scrollTop(true);
		};
		
		$ionicModal.fromTemplateUrl('templates/modal/file_list.html', {
			scope: $scope,
		}).then(function(modal) {
			$scope.agentModal = modal;
		});
		$scope.$on('$destroy', function() {
			if($scope.agentModal)
				$scope.agentModal.remove();
		});
		$scope.filelist_close = function() {
			$scope.agentModal.hide();
		}
		$scope.doShowFile = function(order_id, act_file_id, act_type) {
			$scope.order_id = order_id;
			$scope.act_type = act_type;
			$scope.agentModal.show();
			$scope.initfile(act_file_id);
		}
		$scope.initfile = function(act_file_id) {
			//根目录
			FileSystem.readFileSystemRoot(act_file_id, function(data) {
				$scope.files = new Array();
				for(var i = 0; i < data.length; i++) {
					if(data[i].isFile)
						$scope.files.push(data[i]);
				}
			}, function(e) {
				GoDialog.alert('提示', '获取列表失败');
			});
			//			console.log($scope.files);
		};
		//打开文件
		$scope.openfile = function(item) {
			var type = item.name.split('.');
			type = type[type.length - 1];
			if(type == 'doc' || type == 'docx') {
				type = 'application/msword';
			} else if(type == 'xls' || type == 'xlsx') {
				type = 'application/vnd.ms-excel';
			}
			FileSystem.fileOpenWithDialog(item.nativeURL, type, function() {
				GoDialog.alert('提示', '打开文件成功');
			}, function(e) {
				GoDialog.alert('提示', '打开文件失败');
			});
		};
		$scope.file_checked = function(index) {
			for(var i = 0; i < $scope.files.length; i++) {
				if(i != index) {
					$scope.files[i].check = false;
				}
			}
		}
		$scope.doUploadFile = function(val) {
			for(var i = 0; i < $scope.files.length; i++) {
				if($scope.files[i].check == true) {
					FileSystem.uploadFile($scope.files[i].name, $scope.order_id, $scope.act_type, function(data) {
						if(data.success == true) {
							$scope.file_id = data.file_id;
							$scope.filename = data.local_name;
							$scope.agentModal.hide();
						}
						GoDialog.alert('提示', data.msg);
					}, function(erro) {
						GoDialog.alert('提示', '上传失败');
					});

				}
			}

		};
		$scope.file_download = function(file_id) {
			FileSystem.downloadFile(file_id, function(data) {
				if(data.success == true) {
					GoDialog.alert('下载提示', '下载成功！请在[上传问卷]中查看已下载的文件,并确保您已安装阅读软件。如需编辑，请在电脑上进行修改');
				} else
					GoDialog.alert('下载提示', data.msg);
			}, function(e) {
				GoDialog.alert('下载提示', '下载失败');
			})
		}

	}

	controller.$inject = ['$scope', 'Afall', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', 'Auth', '$window', '$timeout', '$stateParams', '$ionicScrollDelegate', 'FileSystem', '$ionicModal', '$ionicPopup'];

	return app.registerController("afallctrl", controller);
});